package com.gjs.freechat.server.component;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gjs.freechat.server.component.bean.ChatSession;
import com.gjs.freechat.server.component.bean.Conversation;
import com.gjs.freechat.server.component.bean.Message;
import com.gjs.freechat.server.component.bean.UserInfo;
import com.gjs.freechat.server.dao.ChatSessionDao;
import com.gjs.freechat.server.dao.mapper.ChatSessionMapper;
import com.gjs.freechat.utils.DateUtil;

/**
 * 聊天会话组件层
 * @author alienware
 *
 */
@Service
public class ChatSessionManage {
	
	public static String IMAGE_STORE_LOCATION = "D://images/";
	public static String IMAGE_REQUEST_LOCATION = "http://localhost:8080/photofreechat/";
	
	@Autowired
	ChatSessionDao chatSessionDao;
	
	@Autowired
	ChatSessionMapper chatSessionMapper;
	
	@Autowired
	UserSessionPool convosPool;
	
	public ChatSessionManage() {
	}
	
	public Map<String, Object> getAllConvosWithStatistic(String userId) {
//		List<Conversation> convos = convosPool.getExistedConvos(userId);
//		if (convos == null || convos.size() == 0)		
//			convos = chatSessionMapper.getAllConvos(userId);
		List<Conversation> convos = chatSessionMapper.getAllConvos(userId);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("allConvos", convos);
		int friend = 0, online = 0;
		for (Conversation convo : convos) {
			if (convo.getType() == Conversation.CONVO_TYPE_FRIEND) {
				friend++;
			}
			if (convo.getPartnerStatus() == UserInfo.USER_STATUS_ONLINE) {
				online++;
			}
		}
		Collections.sort(convos, new Comparator<Conversation>() {
			@Override
			public int compare(Conversation o1, Conversation o2) {
				return o2.getLastEventTime().compareTo(o1.getLastEventTime());
			}
		});
		result.put("allConvosCount", convos.size());
		result.put("friendConvosCount", friend);
		result.put("onlineConvosCount", online);
		return result;
	}
	
	public List<Conversation> getAllConvos(String userId) {
		return chatSessionMapper.getAllConvos(userId);
	}
	
	public List<ChatSession> getAllChatSessions() {
		return chatSessionMapper.getAllChatSessions();
	}
	
	
	public Map<String, Object> createNewChatSession(String sessionId, Integer type, UserInfo user, UserInfo partner) {
		ChatSession chatSession = new ChatSession(sessionId, type, user.getId(), partner.getId());
		chatSessionMapper.addNewSession(chatSession);
		String createTime = DateUtil.formatDate(new Date());
		Conversation convo = new Conversation(sessionId, type, user.getId(), partner, 0);
		convo.setCreateTime(createTime);
		chatSessionMapper.addNewConvo(convo);
		Conversation partnerConvo = new Conversation(sessionId, type, partner.getId(), user, 0);
		partnerConvo.setCreateTime(createTime);
		chatSessionMapper.addNewConvo(partnerConvo);
		Map<String, Object> sessionMap = new HashMap<String, Object>(3);
		sessionMap.put("chatSession", chatSession);
		sessionMap.put("userConvo", convo);
		sessionMap.put("partnerConvo", partnerConvo);
		return sessionMap;
	}
	
	
	public void addNewConvo(Conversation convo) {
		chatSessionMapper.addNewConvo(convo);
	}
	
	public void addNewSession(ChatSession chatSession) {
		chatSessionMapper.addNewSession(chatSession);
	}
	
	public void updateConvo(Conversation convo, String sessionId) {
		chatSessionMapper.updateConvo(convo, sessionId, convo.getUserId());
	}
	
	public void saveMessage(Message msg) {
		chatSessionMapper.insertNewMessage(msg);
	}
	
	public void addUnreadMessage(String sessionId, String userId) {
		chatSessionMapper.addUnreadMessage(sessionId, userId);
	}
	
	public void clearUnreadMessages(String sessionId, String userId) {
		chatSessionMapper.clearUnreadMessages(sessionId, userId);
	}

	public void removeConvo(String userId, String sessionId) {
		chatSessionMapper.removeConvo(sessionId, userId);
	}
	
	public void removeConvo(String sessionId) {
		chatSessionMapper.removeConvo(sessionId, null);
	}
	
	public void removeSession(String sessionId) {
		chatSessionMapper.removeConvo(sessionId, null);
		chatSessionMapper.removeSession(sessionId);
		chatSessionMapper.removeMessages(sessionId);
	}
	
	public void removeMessageByMsgType(Message msg) {
		chatSessionMapper.removeMessageByMsgType(msg);
	}
	
	public void addFriendRequest(String sessionId, String userId) {
		chatSessionMapper.addFriendRequest(sessionId, userId);
	}
	
	public void rejectFriendRequest(String sessionId) {
		chatSessionMapper.rejectFriendRequest(sessionId);
	}
	
	public void acceptFriendRequest(String sessionId) {
		chatSessionMapper.acceptFriendRequest(sessionId);
	}
	
	public void updateFriendNickName(String convoId, String userId, String name) {
		chatSessionMapper.updateFriendNickName(convoId, userId, name);
	}
	
}
